package com.eboss.sbs.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.WalletDrawAccountInfo;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyDialog;

import java.util.HashMap;
import java.util.Map;

public class MyWalletDrawActivity extends BaseActivity {

	private PullToRefreshScrollView scroolView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_my_wallet_draw);
		super.onCreate(savedInstanceState);
		initView();
		getdata();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "申请提款");
		
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.tv_submit).setOnClickListener(this);
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements OnRefreshListener<ScrollView>{
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		API.http_post(this, API.WALLET_DRAW_ACCOUNT, params, new HttpCallback<WalletDrawAccountInfo>(this) {
			@Override
			public void onSuccess(int statusCode, WalletDrawAccountInfo data) {
				scroolView.onRefreshComplete();
				setTvText(R.id.tv_balance, ""+ StringUtil.getMoneyString(data.getBalance()));
				setTvText(R.id.tv_bank, ""+data.getBank());
				setTvText(R.id.tv_accountName, ""+data.getAccountName());
				setTvText(R.id.tv_accountNo, ""+data.getAccountNo());
				setTvText(R.id.tv_branchName, ""+data.getBranchName());
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				
				WalletDrawAccountInfo data_tmp = new WalletDrawAccountInfo(123123, "中国银行", "白小白", "1234567890", "上海浦东支行");
				setTvText(R.id.tv_balance, ""+StringUtil.getMoneyString(data_tmp.getBalance()));
				setTvText(R.id.tv_bank, ""+data_tmp.getBank());
				setTvText(R.id.tv_accountName, ""+data_tmp.getAccountName());
				setTvText(R.id.tv_accountNo, ""+data_tmp.getAccountNo());
				setTvText(R.id.tv_branchName, ""+data_tmp.getBranchName());
				
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<WalletDrawAccountInfo>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.tv_submit:
				submit();
				break;
			case R.id.ly_draw_log:
				
				break;
			case R.id.ly_draw_accounts:
				
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	private void submit(){
		final String drawMoney = getTvText(R.id.tv_draw_money);
		if(StringUtil.isNull(drawMoney)){toast("请输入提现金额");return;}
		MyDialog.Builder builder = new MyDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("请确认是否申请提现"+drawMoney+"元金额到银行账户。");
		builder.setNegativeButton("继续修改", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setPositiveButton("确认无误", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				submit_ext(drawMoney);
			}
		});
		builder.create().show();
	}
	
	private void submit_ext(String drawMoney){
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("drawMoney", drawMoney);
		API.http_post(this, API.WALLET_DRAW_ACCOUNT, params, new HttpCallback<WalletDrawAccountInfo>(this) {
			@Override
			public void onSuccess(int statusCode, WalletDrawAccountInfo data) {
				scroolView.onRefreshComplete();
				toast("提交成功");
				setResult(RESULT_OK);
				finish();
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<WalletDrawAccountInfo>() {
				};
			}
		});
	}
	
}
